package com.j.thetabk;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.mtp.MtpDevice;
import android.mtp.MtpObjectInfo;
import android.net.Uri;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class MTPTransferService extends Service {

    protected MtpDevice mtpDevice=null;

    protected NotificationManager mNotificationManager = null;

    protected NotificationCompat.Builder builder=null;

    private static final int SERVICE_ID = 79290;

    public class MyThread2 extends Thread
    {
        private int stid=0;
        public MyThread2(int startId) {
            stid=startId;
        }

        public MTPTransfer.MTPTransferUpdate mtptu=new MTPTransfer.MTPTransferUpdate() {
            @Override
            public void OnUpdate(MtpObjectInfo mtpObjectInfo, int status) {

            }

            @Override
            public void TrackFile(String path, long size, boolean bMove) {

            }

            @Override
            public void UpdateFile(String path, long size, int status) {
                switch (status)
                {
                    case 0:
                        currentFP=path;
                        fileSize=size;
//                        statusStr=setup.isbMove()?"moving":"copying";
                        statusStr="";
                        break;
                    case 1:
                        currentFP=path;
                        fileSize=size;
                        statusStr="complete";
                        remainCount--;
                        remainByte-=size;
                        break;
                    case 2:
                        currentFP=path;
                        fileSize=size;
                        statusStr="fail";
                        remainCount--;
                        remainByte-=size;
                        break;
                    case 3:
                        currentFP=path;
                        fileSize=size;
                        statusStr="skip";
                        remainCount--;
                        remainByte-=size;
                        break;
                    default:
                        break;
                }

            }

            @Override
            public void UpdateTotal(long count, long size) {
                remainCount=count;
                remainByte=size;
                builder.setContentTitle((setup.isbMove()?"moving ":"copying ")+remainCount+" item["+MTPTransfer.SizeStrD(remainByte)+"]");

                new NoticeThread().start();
            }
        };

        @Override
        public void run() {
            super.run();

            MTPTransfer.ScanTransfer2(mtpDevice, setup.getSavePath(),setup.isbMove(),setup.ToSelect(),mtptu);

            remainCount=0;

            if(setup.isbImage())
                try {
                    MTPTransfer.GroupMove(setup.getSavePath()+"/DCIM",setup.getBkFolder());
                } catch (IOException e) {
                    e.printStackTrace();
                }

            if(false)
            {
                Uri startDir = Uri.fromFile(new File(setup.getSavePath()));
                Intent intent = new Intent();
                intent.setData(startDir);
                intent.setType("*/*");
                intent.setAction(Intent.ACTION_VIEW);
                startActivity(intent);
            }


            stopSelfResult(stid);
            Log.v("MyService","Thread2 end");
        }
    }


    protected long remainCount=0;
    protected String currentFP="";
    protected long fileSize=0;
    protected String statusStr="";
    protected long remainByte=0;

    public class NoticeThread extends Thread
    {
        @Override
        public void run() {
            super.run();

            while (remainCount>0)
            {
                File file=new File(currentFP);
                long csz=0;
                if(file.exists())
                {
                    csz=file.length();
                }
                builder.setContentText(statusStr+" "+file.getName()+"["+MTPTransfer.SizeStrD(fileSize)+"]");
//                Log.v("NoticeThread",statusStr+" "+currentFP+"["+MTPTransfer.SizeStrD(fileSize)+"]"+remainCount);
                builder.setNumber((int)remainCount);

                builder.setProgress((int)(fileSize/2),(int)(csz/2),false);
                mNotificationManager.notify(SERVICE_ID, builder.build());

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            Log.v("MyService","NoticeThread end");
        }
    }

    protected TransferSetup setup=null;

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        mtpDevice=TransferActivity.mtpDevice;

        setup=(TransferSetup)intent.getSerializableExtra("setup");

        builder = new NotificationCompat.Builder(getApplicationContext());
//        builder.setContentTitle("ContentTitle");
        builder.setContentText("counting item");
        builder.setSmallIcon(R.mipmap.ic_launcher);
//        builder.setContentIntent(pIntent);
//        builder.setColor(0x00FF0000);
//        builder.setNumber(0);
        builder.setProgress(100,0,true);
//        builder.setSubText("SubText");
//        builder.setTicker("Ticker");

        startForeground(SERVICE_ID, builder.build());

        mNotificationManager=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        new MyThread2(startId).start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
}
